Repository navigation
zlib,url: avoid flattening strings in value-returning fast API calls - #67536
Open
dasunNimantha wants to merge 3 commits into
Open
dasunNimantha wants to merge 3 commits into
dasunNimantha wants to merge 3 commits into
Conversation
Collaborator
|
Review requested:
|
Contributor
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. |
zlib.crc32() and zlib.crc32c() read string input through a V8 fast API callback that returns a value. Reading a non-flat (cons) string flattens it, which allocates on the JS heap. If that allocation triggers a full GC that invalidates the optimized caller, V8 has to lazily deoptimize it after the fast call returns, which it does not support for fast calls with a return value: the process aborts with "Lazy deopt after a fast API call with return value is unsupported". Strings now go through a regular binding. The fast bindings are limited to ArrayBufferViews, which are read without allocating. Refs: nodejs#66870 Refs: nodejs#67136 Signed-off-by: dasunNimantha <dasun4@pm.me>
URL.canParse() converted and flattened its arguments inside a V8 fast API callback that returns a value. Like zlib.crc32(), that can allocate and trigger a GC that invalidates the optimized caller, which V8 then cannot lazily deoptimize and aborts instead. The fast path now takes a sequential one-byte string, which V8 passes without allocating, and returns -1 for anything it cannot handle that way so that JavaScript falls back to the regular binding. Refs: nodejs#66870 Refs: nodejs#67136 Signed-off-by: dasunNimantha <dasun4@pm.me>
Optimize a caller of Buffer.byteLength(), zlib.crc32(), zlib.crc32c() and URL.canParse() that embeds a map weakly, drop the only object with that map, and call it with non-flat old-space strings so that a full GC runs while the string is read. Without the fixes the process aborts with "Lazy deopt after a fast API call with return value is unsupported". Refs: nodejs#66870 Signed-off-by: dasunNimantha <dasun4@pm.me>
dasunNimantha
force-pushed
the
fast-api-no-gc
branch
from
November 17, 2026 06:15
e532d05 to
e2157b9
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #67536 +/- ##
=======================================
Coverage 90.67% 90.68%
=======================================
Files 800 800
Lines 280489 280516 +27
Branches 54166 54171 +5
=======================================
+ Hits 254336 254384 +48
+ Misses 16277 16253 -24
- Partials 9876 9879 +3
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zlib.crc32(),zlib.crc32c()(string input) andURL.canParse()can abort the process with:This is the same crash as #66870, which #67136 fixed for
Buffer.byteLength(). The fast callbacks of these APIs flatten or convert their string argument (Utf8Value/String::ValueView, andToString()incanParse). Flattening a large non-flat string allocates; if that allocation triggers a full GC that invalidates the optimized caller (for example by clearing a weakly embedded map), V8 has to lazily deoptimize the caller once the fast call returns. V8 does not support that for fast calls that return a value and runsBuiltin::kAbortinstead (crbug.com/418936518, seeFastApiCallReducerAssembler::ReduceFastApiCallindeps/v8/src/compiler/js-call-reducer.cc).Changes:
crc32()/crc32c()now goes through a regular binding. The fast bindings only take ArrayBufferViews, which are read without allocating.canParse()fast path now takes a sequential one-byte string (FastOneByteString), which V8 passes without allocating, and returns-1for anything else so JavaScript calls the regular binding, the same approach as buffer: limit UTF-8 fast calls to safe one-byte strings #67136.ToString()is no longer called inside a fast callback.test/parallel/test-fast-api-calls-gc-lazy-deopt.jsoptimizes a caller of each API, drops the only object with a weakly embedded map, and calls it with non-flat old-space strings so that a full GC runs while the string is read. It aborts on v27.0.0-alpha.0.1.0 and passes with this change.I reproduced the abort on 25.0.0 to 26.13.1 and on v27.0.0-alpha.0.1.0 (
crc32andcanParse;byteLengthis already fixed there by #67136).One note on
URL.canParse(): the interpreter only ever sees the slow callback's-1, so the first optimized call at a call site deopts once (insufficient feedback for theresult === 1compare) and is then re-optimized.canParse()gets faster for ASCII input, since the fast path no longer converts or copies the string.crc32()/crc32c()are unchanged for buffers and large strings; very short string inputs may be somewhat slower because strings no longer take the fast call. These numbers come frombenchmark/compare.jswith 8 runs on a laptop, so small differences are noise; a benchmark CI run would give firmer numbers.permission.has()uses the same pattern (Utf8Valuein a value-returning fast callback), but I have not reproduced the crash there, so it is not part of this PR.AI disclosure: I used a coding agent to help with the root-cause analysis and the initial patch. I reviewed the change, reproduced the crash on the versions above, checked that the new test fails without the fix, and ran
make lint,make format-cpp, and the zlib, url andBuffer.byteLengthtests (parallel/test-zlib*,parallel/test-whatwg-url*,parallel/test-url*,parallel/test-buffer-bytelength*). I have not run the fullmake -j4 test.Refs: #66870
Refs: #67136